import { defineStore } from "pinia";
import { ref } from "vue";

/**
 * @typedef {Object} shopModel
 * @property {string} shop_name - 店铺名字
 * @property {string} shop_avatar - 店铺头像
 * @property {string} shop_desc - 店铺描述
 * @property {string} code - 店铺客服二维码
 */

export const useShopStore = defineStore( 'shop', ( ) => {

	/**
	 * @type {import('vue').Ref<shopModel>}
	 */

	// 店铺信息
	const shopInfo = ref( {
		shop_name: '圆星科技',
		shop_avatar: 'https://mp-25cc8794-9056-4536-a51b-6c5ab0bc5bd9.cdn.bspapp.com/logo.png',
		shop_desc: '欢迎光临',
		code: ''
	} )

	/**
	 * 更新商店信息
	 * 
	 * @param {ShopModel} obj - 需要更新的商店信息对象
	 * @returns {void}
	 */

	const setShopInfo = ( obj ) => {
		shopInfo.value = { ...shopInfo.value, ...obj }
	}

	// 清除用户信息
	const clearnShopInfo = ( ) => {
		// 恢复为未登录的默认值
		shopInfo.value = {
			shop_name: '渔港鱼',
			shop_avatar: 'https://mp-17ba0563-da01-48e9-b9ed-fb1ec160fe94.cdn.bspapp.com/shopAvatar.jpg',
			shop_desc: '欢迎光临',
			code: ''
		}
	}

	return {
		shopInfo,
		setShopInfo,
		clearnShopInfo
	}
}, {
	// 网页端配置
	// persist: true,

	// 小程序端配置
	persist: {
		storage: {
			getItem( key ) {
				return uni.getStorageSync( key )
			},
			setItem( key, value ) {
				uni.setStorageSync( key, value )
			},
		},
	},
} )